
Imports std
Imports System.Web
Imports System.IO

Module main

    Public Class encodingName
        Public Const utf8 As String = "utf8"
        Public Const utf8bom As String = "utf8bom"
        Public Const unicode As String = "unicode"
        Public Const beunicode As String = "beunicode"
        Public Const ascii As String = "ascii"
    End Class

    Sub Main(ByVal args() As String)
        On Error Resume Next
        Dim reader As TextReader = Nothing
        Dim writer As TextWriter = Nothing
        Dim input As String = Nothing
        Dim argIndex As Int32 = 0
        Dim i As Int32
        Dim outputEncoding As String = Nothing
        Dim enc As Text.Encoding = Nothing

        If args.Length < 2 Then
            raiseError("argument count error, urlencoding [-o <outputEncoding>] input output, " + _
                        "internal encoding support " + encodingName.utf8 + _
                        " " + encodingName.utf8bom + _
                        " " + encodingName.unicode + _
                        " " + encodingName.beunicode + _
                        " " + encodingName.ascii + ", default output is " + encodingName.utf8 + _
                        ", other support based on environment. if input or output is /-, use console.", _
                        errorHandle.errorType.critical)
        Else
            For i = 0 To args.Length - 3 Step 2
                If strsame(args(i), "-o", False) Then
                    outputEncoding = args(i + 1)
                End If
            Next

            If strsame(outputEncoding, encodingName.utf8bom) Then
                enc = encoding.utf8bom
            ElseIf strsame(outputEncoding, encodingName.unicode) Then
                enc = encoding.unicode
            ElseIf strsame(outputEncoding, encodingName.beunicode) Then
                enc = encoding.beunicode
            ElseIf strsame(outputEncoding, encodingName.ascii) Then
                enc = encoding.ascii
            ElseIf strsame(outputEncoding, encodingName.utf8) Then
                enc = encoding.utf8
            Else
                enc = System.Text.Encoding.GetEncoding(outputEncoding)
            End If
            If enc Is Nothing Then
                enc = encoding.utf8
            End If

            If strsame(args(args.Length() - 2), "/-", False) Then
                reader = Console.In
            Else
                reader = New StreamReader(
                         New FileStream(args(args.Length() - 2), FileMode.Open, FileAccess.Read, FileShare.Read))
            End If
            If strsame(args(args.Length() - 1), "/-", False) Then
                writer = Console.Out
            Else
                writer = New StreamWriter(
                         New FileStream(args(args.Length() - 1), FileMode.Create, FileAccess.Write, FileShare.Read))
            End If

            assert(Not reader Is Nothing, "file no found, " + args(args.Length() - 2))
            assert(Not writer Is Nothing, "file no found, " + args(args.Length() - 1))
            input = reader.ReadLine
            While Not input Is Nothing
                writer.WriteLine(HttpUtility.UrlEncode(input, enc))
                input = reader.ReadLine()
            End While

            closestream(reader)
            closestream(writer)
        End If
    End Sub

End Module
